package com.dhms.vostok1.service.ble

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanFilter
import android.bluetooth.le.ScanSettings
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import com.dhms.lib.app.LibApplication
import java.util.*

abstract class BleService(context: Context) {

    private var topScanTimer: Timer? = null//扫描计时器
    protected var bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager//获取Android 蓝牙管理器
    protected var btAdapter: BluetoothAdapter? = bluetoothManager.adapter//蓝牙适配器


    /**
     * 扫描蓝牙设备
     * @param filter 需要过滤的扫描参数
     * @param leShortScanCallback 回调参数
     */
    fun scan(filter: List<ScanFilter>?, leShortScanCallback: ScanCallback,) {
        // the scan operation must to be stopped to save battery
        topScanTimer = Timer()
        topScanTimer?.schedule(object : TimerTask() {
            override fun run() {
                if (ActivityCompat.checkSelfPermission(
                        LibApplication.getAppContext(),
                        Manifest.permission.BLUETOOTH_SCAN
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return
                }
                btAdapter?.bluetoothLeScanner?.stopScan(leShortScanCallback)//计时器结束停止扫描
            }
        }, 10 * 1000)
        //开始扫描
        btAdapter?.bluetoothLeScanner?.startScan(
            filter,
            ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(),
            leShortScanCallback
        )
    }

    /**
     * 手动停止扫描
     * @param
     */
    @SuppressLint("MissingPermission")
    fun stopScan(leShortScanCallback: ScanCallback) {
        topScanTimer?.cancel()
        topScanTimer = null
        btAdapter?.bluetoothLeScanner?.stopScan(leShortScanCallback)
    }

    /**
     * 连接设备
     * @param device 蓝牙设备
     */
    abstract fun connectDevice(device: BluetoothDevice)

    /**
     * 关闭连接
     */
    abstract fun closeConnect()

    /**
     * 更新设备
     */
    abstract fun upgrade(device: BluetoothDevice, fileName: String, versionNum: Int, firmware: ByteArray)

    /**
     * 请求电量
     */
    abstract fun requestPowerLevel()

    /**
     * 获取设备信息
     * @param device 蓝牙设备
     */
    abstract fun requestDeviceInfo(device: BluetoothDevice)

    /**
     * 获取温度
     */
    abstract fun requestTemperature()

    /**
     * 获取震动分析
     */
    abstract fun requestVibAnalysis()

    /**
     * 获取震动数据
     * @param sample
     * @param sampleRate
     * @param speed
     */
    abstract fun requestVibData(sample: Long, sampleRate: Long, speed: Long)
}